package org.example.service.Impl;

import lombok.extern.slf4j.Slf4j;
import org.example.dao.BookingRecordDao;
import org.example.dao.InoculationRecordDao;
import org.example.dao.UserDao;
import org.example.domain.User;
import org.example.service.UserService;
import org.example.utils.ResponseUtil.ResponseResult;
import static org.example.utils.ResponseUtil.StatusCode.*;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;


@Service
@Slf4j
public class UserServiceImpl implements UserService {

    @Resource
    private UserDao userDao;

    //疫苗接种记录
    @Resource
    private InoculationRecordDao inoculationRecordDao;

    //疫苗预约记录
    @Resource
    private BookingRecordDao bookingRecordDao;



    /**
     * 账号登录验证
     */
    @Override
    public ResponseResult selectUserByAccountAndPassword(User user) {
        user = userDao.selectUserByAccountAndPassword(user);
        //表示账号或密码错误
        if (user == null){
            return new ResponseResult(DATA_NOT_EXIST,"登录失败!账号或密码错误");
        }
        //账号的状态码=0，表示此账号已被注销
        if (user.getStatus() == 0){
            return new ResponseResult(DATA_NOT_EXIST,"登录失败!此账号已注销");
        }

        ResponseResult responseResult = null;
        //若当前账号和密码均正确
        //身份为管理员时：user.getRole()==1
        if (user.getRole()==1){
            responseResult = new ResponseResult(SERVICE_RUN_SUCCESS,user,
                    "登录成功，'"+user.getAccount()+"' 管理员您好!");
        }
        //身份为普通用户时：user.getRole()==2
        else if(user.getRole()==2){
            responseResult = new ResponseResult(SERVICE_RUN_SUCCESS,user,
                    "登录成功，'"+user.getAccount()+"' 用户您好！");
        }
        else {
            responseResult = new ResponseResult(SERVICE_RUN_SUCCESS,user,
                    "登录成功，'" + user.getAccount() + "' 身份不明！");
        }
        return responseResult;

    }


    /**
     * 增加：新账号注册功能
     */
    @Override
    public ResponseResult insertUser(User user) {
        //首先根据账号去数据库查找是否存在重复账号
        User u = userDao.selectUserByAccount(user.getAccount());
        //如果当前账号重复
        if (u != null){
            return new ResponseResult(DATA_NOT_EXIST,
                    "注册失败，'"+user.getAccount() +"' 账号已存在！");
        }
        //否则执行插入操作
        else {
            //执行数据的插入操作
            int n = userDao.insertUser(user);
            return new ResponseResult(SERVICE_RUN_SUCCESS,user,
                    user.getAccount() +" 账号，注册成功！");
        }
    }


    /**
     * 修改：根据账号id，更新其对应的账号信息
     */
    @Override
    public ResponseResult updateUserById(User user) {
        //获取前端传过来的账号id
        Integer id = user.getUid();
        if (id==null || id == 0){
            return new ResponseResult(DATA_NOT_EXIST, "更新失败，账号的id值为空！");
        }

        //首先需要判断，想要更新的新账号是否已经存在
        User user1 = userDao.selectUserByAccount(user.getAccount());
        //如果当前账号存在，且id与当前需要更新的id号不同，即表示有两个id不同但account却相同的账号
        if (user1 != null && user1.getUid() != id){
            return new ResponseResult(DATA_NOT_EXIST, user.getAccount() +" 账号已存在，更新失败！");
        }

        //如果不存在重复的account
        user1 = userDao.selectUserById(id);//根据前端给的id去数据库查找对应的对象
        //如果前端传来对象的某个属性值为空，就把数据库中的原值存进去
        if (user.getAccount()==null || "".equals(user.getAccount())){
            user.setAccount(user1.getAccount());
        }
        if (user.getUsername()==null || "".equals(user.getUsername())){
            user.setUsername(user1.getUsername());
        }
        if (user.getPassword()==null|| "".equals(user.getPassword())){
            user.setPassword(user1.getPassword());
        }
        if (user.getDept()==null|| "".equals(user.getDept())){
            user.setDept(user1.getDept());
        }
        if (user.getRole()==null || user.getRole() == 0){
            user.setRole(user1.getRole());
        }
        if (user.getStatus()==null ){
            user.setStatus(user1.getStatus());
        }


        //执行真正的更新操作
        int n = userDao.updateUserById(user);
        if (n>0){
            return new ResponseResult(SERVICE_RUN_SUCCESS,user,"用户信息更新成功！");
        }
        else {
            return new ResponseResult(DATA_NOT_EXIST, "数据库提示：更新失败！");
        }
    }


    /**
     * 物理删除：根据id删除对应账号.
     * 同时，疫苗接种记录进行逻辑删除：status=0，
     * 疫苗预约记录进行物理删除。
     */
    @Override
    public ResponseResult deleteUserById(Integer uid) {
        int n = userDao.deleteUserById(uid);
        if (n > 0){
            //将此账号相关的 接种记录，进行逻辑删除， status=0
            inoculationRecordDao.deleteInoculationByUserId(uid);
            //将此账号相关的 预约记录，进行物理删除
            bookingRecordDao.deleteBookingRecordByUserId(uid);
            return new ResponseResult(SERVICE_RUN_SUCCESS,n,"id="+uid+" 的账号删除成功！");
        }
        else {
            return new ResponseResult(DATA_NOT_EXIST,"id="+uid+" 的账号删除失败！");
        }
    }


    /**
     * 逻辑删除：根据id注销对应账号，status=0。
     * 疫苗接种记录不做修改，
     * 疫苗预约记录进行物理删除。
     */
    @Override
    public ResponseResult closeUserById(Integer uid) {
        int n = userDao.closeUserById(uid);
        if (n > 0){
            //将此账号相关的 预约记录，进行物理删除
            bookingRecordDao.deleteBookingRecordByUserId(uid);
            return new ResponseResult(SERVICE_RUN_SUCCESS,n,"id="+uid+" 的账号注销成功！");
        }
        else {
            return new ResponseResult(DATA_NOT_EXIST,"id="+uid+" 的账号注销失败！");
        }
    }



    /** ================= 以下是 根据某一字段进行 模糊查询==============    */
    @Override
    public ResponseResult selectUserLikeAccount(String account) {
        List users = userDao.selectUserLikeAccount(account);
        if (users == null || users.size() == 0){
            return new ResponseResult(DATA_NOT_EXIST,"未找到账号中含有'"+account+"'的账号！");
        }
        else {
            return new ResponseResult(SERVICE_RUN_SUCCESS,users,"已找到账号中含有'"+account+"'的账号！");
        }
    }


    @Override
    public ResponseResult selectUserLikeUsername(String username) {
        List users = userDao.selectUserLikeUsername(username);
        if (users == null || users.size() == 0){
            return new ResponseResult(DATA_NOT_EXIST,"未找到用户名中含有'"+username+"'的账号！");
        }
        else {
            return new ResponseResult(SERVICE_RUN_SUCCESS,users,"已找到用户名中含有'"+username+"'的账号！");
        }
    }


    @Override
    public ResponseResult selectUserLikeDept(String dept) {
        List users = userDao.selectUserLikeDept(dept);
        if (users == null || users.size() == 0){
            return new ResponseResult(DATA_NOT_EXIST,"未找到与'"+dept+"'部门相关的账号！");
        }
        else {
            return new ResponseResult(SERVICE_RUN_SUCCESS,users,"已找到与'"+dept+"'部门相关的账号！");
        }
    }



    /** ================= 以下是 根据某一字段进行 精确查找 ==============    */
    @Override
    public ResponseResult selectUserById(Integer uid) {
        User user = userDao.selectUserById(uid);
        if (user == null){
            return new ResponseResult(DATA_NOT_EXIST,"id="+uid+" 的账号不存在");
        }
        else {
            List list = new ArrayList();
            list.add(user);
            return new ResponseResult(SERVICE_RUN_SUCCESS,list,"id="+uid+" 的账号存在");
        }
    }


    @Override
    public ResponseResult selectUserByAccount(String account) {
        User user = userDao.selectUserByAccount(account);
        if (user == null){
            return new ResponseResult(DATA_NOT_EXIST,"账号'"+account+"' 不存在！");
        }
        else {
            List list = new ArrayList();
            list.add(user);
            return new ResponseResult(SERVICE_RUN_SUCCESS,list,"账号'"+account+"'已存在！");
        }
    }


    @Override
    public ResponseResult selectUserByUsername(String username) {
        List users = userDao.selectUserByUsername(username);
        if (users == null || users.size() == 0){
            return new ResponseResult(DATA_NOT_EXIST,"用户名为'"+username+"'的账号不存在！");
        }
        else {
            return new ResponseResult(SERVICE_RUN_SUCCESS,users,"用户名为'"+username+"'的账号存在！");
        }
    }


    @Override
    public ResponseResult selectUserByDept(String dept) {
        List users = userDao.selectUserByDept(dept);
        if (users == null || users.size() == 0){
            return new ResponseResult(DATA_NOT_EXIST,"所在单位为'"+dept+"'的账号不存在！");
        }
        else {
            return new ResponseResult(SERVICE_RUN_SUCCESS,users,"所在单位为'"+dept+"'的账号存在！");
        }
    }


    @Override
    public ResponseResult selectUserByRole(Integer role) {
        String s = "";
        if (role == 1) s = "管理员";
        else if (role == 2) s = "普通";

        List users = userDao.selectUserByRole(role);
        if (users == null || users.size() == 0){
            return new ResponseResult(DATA_NOT_EXIST,s+"账号不存在！");
        }
        else {
            return new ResponseResult(SERVICE_RUN_SUCCESS,users,s+"账号存在！");
        }
    }

    @Override
    public ResponseResult selectUserByStatus(Integer status) {
        String s = "";
        if (status == 1) s = "可用";
        else if (status == 0) s = "停用";

        List users = userDao.selectUserByStatus(status);
        if (users == null || users.size() == 0){
            return new ResponseResult(DATA_NOT_EXIST,"不存在 "+s+"账号！");
        }
        else {
            return new ResponseResult(SERVICE_RUN_SUCCESS,users,"存在 "+s+"账号！");
        }
    }


    /** 查找所有账号   */
    @Override
    public ResponseResult selectAllUser() {
        List users = userDao.selectAllUser();
        if (users == null || users.size() ==0 ){
            return new ResponseResult(DATA_NOT_EXIST,"账号列表为空！");
        }
        return new ResponseResult(SERVICE_RUN_SUCCESS,users,"账号列表查询成功");

    }

}
